//
//  AmuseMuneView.swift
//  DouYuZB
//
//  Created by 梁森 on 2020/4/25.
//  Copyright © 2020 梁森. All rights reserved.
//

import UIKit

private let kCollectionH:CGFloat = 180.0
private let kPageH:CGFloat = 20.0

class AmuseMuneView: BaseView {
    var anchorGroups:[AnchorGroup]?{
        didSet{
            self.collectionView.reloadData()
        }
    }
    // MARK: 懒加载
    fileprivate lazy var collectionView:UICollectionView = {
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.itemSize = CGSize(width: kScreenWidth, height: kCollectionH)
        flowLayout.scrollDirection = .horizontal
        let collectionFrame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kCollectionH)
        let collectionView = UICollectionView(frame: collectionFrame, collectionViewLayout: flowLayout)
        collectionView.backgroundColor = UIColor.white
        collectionView.register(AmuseMuneCollectionViewCell.self, forCellWithReuseIdentifier: AmuseMuneCollectionViewCell.reuseIdentifier())
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.isPagingEnabled = true
        collectionView.showsHorizontalScrollIndicator = false
        return collectionView
    }()
    fileprivate lazy var pageControl:UIPageControl = {
        let pageFrame = CGRect(x: 0, y: kCollectionH, width: 0, height: kPageH)
        let pageControl = UIPageControl(frame: pageFrame)
        pageControl.numberOfPages = 6
        pageControl.pageIndicatorTintColor = UIColor.darkGray
        pageControl.currentPageIndicatorTintColor = UIColor.orange
        return pageControl
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
// MARK:设置UI
extension AmuseMuneView{
    fileprivate func setupUI(){
        addSubview(collectionView)
        addSubview(pageControl)
    }
}
// MARK:遵守协议
extension AmuseMuneView:UICollectionViewDelegate, UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if anchorGroups == nil {return 0}
        let sectionCount = (anchorGroups!.count - 1) / 8 + 1
        pageControl.numberOfPages = sectionCount
        return sectionCount
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: AmuseMuneCollectionViewCell.reuseIdentifier(), for: indexPath) as! AmuseMuneCollectionViewCell
        cell.backgroundColor = UIColor.white
        
        let startIndex = indexPath.item * 8
        var endIndex = (indexPath.item + 1) * 8 - 1
        if endIndex > anchorGroups!.count - 1 {
            endIndex = anchorGroups!.count - 1
        }
        cell.anchorGroups = Array(anchorGroups![startIndex...endIndex])
        return cell
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        pageControl.currentPage = Int(scrollView.contentOffset.x / scrollView.bounds.width)
    }
    
}
